package com.lottoad.android.cronos.parser;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.lottoad.android.cronos.data.CNLottoAdWinnerData;
import com.lottoad.android.cronos.datahandler.CNBaseDataParser;
import com.lottoad.android.cronos.datahandler.CNResultSet;

public class CNWinnerListParser extends CNBaseDataParser{
	protected void onParseContent(Element xmlObject, CNResultSet resultSet)
			throws Exception {

		NodeList nodeList = xmlObject.getElementsByTagName("winLotto");
		
		NodeList nodeChaSeq = xmlObject.getElementsByTagName("chaSeq"); 
		NodeList nodeLottoNum = xmlObject.getElementsByTagName("lottoNum");
		
		NodeList nodeTotalCnt = xmlObject.getElementsByTagName("totalCnt");
		NodeList nodeWin1 = xmlObject.getElementsByTagName("winTot1");
		NodeList nodeWin2 = xmlObject.getElementsByTagName("winTot2");
		NodeList nodeWin3 = xmlObject.getElementsByTagName("winTot3");
		NodeList nodeWin4 = xmlObject.getElementsByTagName("winTot4");
		NodeList nodeWin5 = xmlObject.getElementsByTagName("winTot5");
		
		NodeList nodeWinTotalCnt = xmlObject.getElementsByTagName("totalCnt");
		String totalWinner = nodeWinTotalCnt.item(0).getFirstChild().getNodeValue();
		resultSet.setTotalWinner(totalWinner);
		
		NodeList totalCntNode = xmlObject.getElementsByTagName("totalCount");
		String totalCnt = totalCntNode.item(0).getFirstChild().getNodeValue();
		resultSet.setTotalCount(Integer.parseInt(totalCnt));
		
		int count = nodeList.getLength();
		
		for (int i = 0; i < count; i++)
		{
			Element elementData = (Element) nodeList.item(i);
			if (elementData == null)
				continue;
			
			CNLottoAdWinnerData dataSet = new CNLottoAdWinnerData();
			
			if(nodeChaSeq.item(i).getFirstChild() != null)
				dataSet.chaSeq = Integer.parseInt(nodeChaSeq.item(i).getFirstChild().getNodeValue());
			
			if(nodeLottoNum.item(i).getFirstChild() != null)
				dataSet.lottoNum = nodeLottoNum.item(i).getFirstChild().getNodeValue();
			
			if(nodeTotalCnt.item(i).getFirstChild() != null)
				dataSet.winnerNum = nodeTotalCnt.item(i).getFirstChild().getNodeValue();
			
			if(nodeWin1.item(i).getFirstChild() != null)
				dataSet.win1Num = nodeWin1.item(i).getFirstChild().getNodeValue();
			
			if(nodeWin2.item(i).getFirstChild() != null)
				dataSet.win2Num = nodeWin2.item(i).getFirstChild().getNodeValue();
			
			if(nodeWin3.item(i).getFirstChild() != null)
				dataSet.win3Num = nodeWin3.item(i).getFirstChild().getNodeValue();
			
			if(nodeWin4.item(i).getFirstChild() != null)
				dataSet.win4Num = nodeWin4.item(i).getFirstChild().getNodeValue();
			
			if(nodeWin5.item(i).getFirstChild() != null)
				dataSet.win5Num = nodeWin5.item(i).getFirstChild().getNodeValue();
			
			resultSet.addDataSet(dataSet);
		}
	}
}
